package net.devkit.core.aspect;

import java.lang.reflect.Method;

import net.devkit.common.toolkit.reflection.ReflectionUtils;
import net.devkit.core.web.BaseController;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

/**
 * 验证码拦截器
 */
//@Aspect
//@Component
public class Dev_01_KaptchaAspect {

	@Around(value="@annotation(net.pm.core.annotation.Kaptcha)")
	public Object validprocess(ProceedingJoinPoint point) throws Throwable {
		final BaseController controller = (BaseController) point.getTarget();

		// 得到拦截的方法
		final Method method = ReflectionUtils.obtainMethod(
				controller, point.getSignature().getName());

		//是否有Kapcha这个注解
		//if (method.getAnnotation(Kaptcha.class) != null) {

			if (controller.obtainCaptchaInRequest() != null
					&& controller.obtainCaptchaInRequest().equals(
							controller.obtainCaptchaInSession())){
				return point.proceed();
			}

			//controller.setSessionMessage("验证码错误!");
			controller.aopAbort = true;				
			if (method.getReturnType() == String.class)
				return "redirect:/error/kaptcha.htm";
			else if (method.getReturnType() == ModelAndView.class)
				return new ModelAndView("redirect:/error/kaptcha.htm");
			else
				return null;

		//}
		//return point.proceed();
	}

}
